<h3>Add data</h3>
<div id="div-objects-combo"></div>
<div id="div-fields"></div>


<script type="text/javascript">

function loadFileds(objName){

	var dataString='&objname='+objName;
	$.ajax({
		type: "POST",
			url: "index.php/soapcontroller/get_fields",
			data: dataString,
			dataType: 'json',
			success : function(data){
				var text='';
				if(data.length > 0){
					console.log(data);
					text='<div class="success">'+data.length+' fields found for the object '+objName+'</div>';
					text=text+'<div class="span-24">&nbsp;</div><table>';
					 text=text+'<tr><th>Field Name</th><th>Datatype</th><th>Reference</th><th>Enter Value</th></tr>';
					$.each(data,function(key,val){
						console.log(val);
						text=text+'<tr><td class="fieldnamecell">'+val.fieldName+'</td>'
						+'<td class="datatypecell">'+val.datatype+'</td>'
						+'<td class="referencecell">'+val.reference+'</td>'
					    +'<td><input type="text" objid="'+val._objid+'" fieldid="'+val._fieldid+'" class="field-values" /></td>'
						+'</tr>';

					});
					 text=text+'<tr><td><button id="add-field-values">Add Values</button></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>'						
					 text=text+'<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>';
					
				}else{
					text='<div class="error">No fields found for the object '+objName+'</div>'; 	
				}
				$('#div-fields').html(text);
				//associateFieldActions();

				$('#add-field-values').click(function(){
					$('.field-values').each(function(){
						var dataString='&objid='+$(this).attr('objid')+
						'&fieldid='+$(this).attr('fieldid')+
						'&fieldval='+$(this).val();

						console.log(dataString);
						$.ajax({
							type: "POST",
								url: "index.php/soapcontroller/adddata",
								data: dataString,
								success : function(data){
									alert(data);
								}
						});
						
					});
		
				});
			}
	});
}

$.getJSON('index.php/soapcontroller/get_all_objects', function(data) {

	   var items = [];
    var text='<select id="objects-combobox"><option value="NONE">--SELECT--</option>';
   
	   $.each(data, function(key, val) {
	      text=text+'<option value="'+val.objName+'">'+val.objName+'</option>';
	      console.log(val._objid+'--'+val.objName);
	   });
	   text=text+'</select>';

	   $('#div-objects-combo').html(text);

	    $('#objects-combobox').change(function(){
		    loadFileds($(this).val());
	    });

});

</script>